package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.family.LoanUnionForeignChannelInfo;
import com.tuanzi.loan.business.service.family.LoanUnionForeignChannelInfoService;
import com.tuanzi.loan.web.bean.CurrentUserBean;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 19:05 2018/11/19
 * Description:
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanUnionForeignChannelInfoBean extends JSFInfoBean<LoanUnionForeignChannelInfo> {
    private static final long serialVersionUID = 1114873006293326705L;

    private final LoanUnionForeignChannelInfoService loanUnionForeignChannelInfoService;

    private final CurrentUserBean currentUserBean;

    private final StringRedisTemplate stringRedisTemplate;

    @Autowired
    public LoanUnionForeignChannelInfoBean(LoanUnionForeignChannelInfoService loanUnionForeignChannelInfoService, CurrentUserBean currentUserBean, StringRedisTemplate stringRedisTemplate) {
        this.loanUnionForeignChannelInfoService = loanUnionForeignChannelInfoService;
        this.currentUserBean = currentUserBean;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @PostConstruct
    private void init() {
        String channelId = getParamFromRequest("channelId");
        String activityId = getParamFromRequest("activityId");
        initChannelInfo(channelId, activityId);
    }

    public void saveInfo(){
        LoanUnionForeignChannelInfo dbChannelInfo = loanUnionForeignChannelInfoService
                .findByChannelIdAndActivityId($.getChannelId(), $.getActivityId());
        if(Objects.nonNull(dbChannelInfo) && null == $.getId()){
            addErrorMessage("渠道与活动已经存在");
            return;
        }

        this.$ = loanUnionForeignChannelInfoService.save($);
        stringRedisTemplate.delete(String.format(LoanRedisKey.FOREIGN_UNION_LOGIN_CHANNEL_INFO,$.getChannelId(), $.getActivityId()));

        changeStatusToView();

        addSuccessfullyMessage();
    }

    private void initChannelInfo(String channelId, String activityId){
        if(StringUtils.isAnyBlank(channelId, activityId)){
            $ = new LoanUnionForeignChannelInfo();
            $.setActivityId("-1");
            changeStatusToEdit();
            return;
        }

        LoanUnionForeignChannelInfo channelInfo = loanUnionForeignChannelInfoService
                .findByChannelIdAndActivityId(Integer.valueOf(channelId), activityId);
        if(Objects.nonNull(channelInfo)){
            $ = channelInfo;
            return;
        }

        forceRedirect(currentUserBean.getBack());
    }
}
